package cn.hchaojie.spring.day01;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.hchaojie.spring.day01.beans.AnnoService;
import cn.hchaojie.spring.day01.beans.AnnotationBean;
import cn.hchaojie.spring.day01.beans.UserBean;
import cn.hchaojie.spring.day01.beans.impl.AnnoServiceImpl;

public class Demo3Annotation {
	ApplicationContext context;
	@Before
	public void init() {
		context = new ClassPathXmlApplicationContext("context-anno.xml");
	}

	@Test
	public void test() {
		AnnotationBean bean = context.getBean(AnnotationBean.class);
		System.out.println(bean);
	}

	/**
	 * 去获取一个未注册的bean
	 */
	@Test
	public void test2() {
		UserBean bean = context.getBean(UserBean.class);
		System.out.println(bean);
	}
	
	@Test
	public void test3() {
		AnnoService service = context.getBean(AnnoService.class);
		service.add();
		service.delete();
	}
	
	/**
	 * 有多个同样类型的bean时候，只能根据名字取bean
	 */
	@Test
	public void test4() {
		AnnoService service1 = (AnnoService) context.getBean("service1");
		service1.add();
		service1.delete();

		AnnoService service2 = (AnnoService) context.getBean("service2");
		service2.add();
		service2.delete();
	}
}
